package com.jerry.huihome.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.module.home.activity.LoreDetailBannerActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.List;

import static com.jerry.huihome.module.home.entity.DrawHouse.BodyBean.BannersBean;

/**
 * Created by LGuipeng on 2016/5/10.
 */
public class HomeBannerAdapter extends PagerAdapter{
    private final Context context;
    private final List<BannersBean> bannersBeanList;
    private final DisplayImageOptions options;

    public HomeBannerAdapter(Context context, List<BannersBean> bannersBeanList){
        this.context = context;
        this.bannersBeanList = bannersBeanList;
        options = BaseApplication.getApplication().getImageOptions();
    }

    @Override
    public int getCount(){
        return bannersBeanList == null ? 0:bannersBeanList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        ImageView imageView = new ImageView(context);
        final BannersBean bannersBean = bannersBeanList.get(position);
        BaseApplication.getApplication()
                .getImageLoader().
                displayImage(bannersBean.getImageUrl(),imageView,options);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(context, LoreDetailBannerActivity.class);
                intent.putExtra("address",bannersBean.getLinkUrl());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object){
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object){
        return view == object;
    }
}
